#ifndef _TOWNS_H_
#define _TOWNS_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "Effects.h"
#include "XMLData.h"

class ItemPtr;
class EffectPtr;
class CharPtr;
class CreatureAIScript;

//物品公共属性
typedef struct town_common_property
{

	Vector3		position;
	uint8		dir;
	
	ResourceProxy		uplink;		//地图 Region 对象
	void initialize(void)
	{
		
		position = Vector3::ZERO;
		dir = 0;
		uplink.setNull();
	}

	town_common_property()
	{
		initialize();
	}

}town_common_property_t;

class  Town:public Resource
{
public:
	enum TownStatus
	{
		TownStatusNone			= 0,	//无效状态
		TownStatusNormal		= 1,	//一般状态
		TownStatusWarPrep		= 2,	//战斗准备状态
		TownStatusWar			= 3,	//战斗状态
		TownStatusProtect		= 4,	//保护状态
		TownStatusDeleted		= 255	//删除状态
	};

	enum TownLayer
	{
		TownLayerNone			= 0,	//无效装备层
		TownLayerWall			= 1,	//城墙
		TownLayerGovt			= 2,	//官府
		TownLayerFarm			= 3,	//农地
		TownLayerCollege		= 4,	//太学殿
		TownLayerHouse			= 5,	//民居
		TownLayerShop			= 6,	//商铺
		TownLayerFactory		= 7,	//工房
		TownLayerArmy			= 8,	//兵营
		TownLayerHotel			= 9,    //酒店
		TownLayerStorage		= 10,	//仓库
		TownLayerWonder			= 11	//奇迹
	};

	Town(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Town(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;

	bool loadChildrenFromDB(void);

private:
	std::list<ResourceProxy>        m_chars;        //城市角色数量
	std::list<ResourceProxy>	m_items;	//包含的物品序列号
	std::list<ResourceProxy>	m_effects;	//城市作用的效果
	
	uint32		m_serial;
	
	String		m_town_name;

	void    initialize(void);

	bool            m_idle;
        uint32          m_timelast;
public:

	const uint32  & getSerial() 	{	return m_serial;	}

	//世界服务器载入子对象，战斗服务器不载入子对象
	bool hasChildren(void);

	//检查是否进行自动更新
        bool    checkUpdate(const uint32 & timenow)     {  return (m_idle && timenow > m_timelast) ? true:false;}
        void    updateTimeLast(const uint32 & timelast) {       m_timelast = timelast;  }
	//设置处理标识为忙，避免重复队列处理
        void    setBusy(void)           {       m_idle = false; }
        void    setIdle(void)           {       m_idle = true;  }
	bool    isBusy(void)            {       return (m_idle == false);       }

	//添加释放城市角色
	bool	addChar(const uint32 & serial);
	bool  	addChar(ResourceProxy & ch);
	bool	removeChar(const uint32 & serial);
	bool	removeChar(ResourceProxy  & ch);
	bool	deleteChar(const uint32 & serial);
	bool	deleteChar(ResourceProxy & ch);
	bool	destroyChar(const uint32 & serial);
	bool	destroyChar(ResourceProxy & ch);
	void	removeAllChars(void);
	void	destroyAllChars(void);
	void	deleteAllChars(void);
	
	//添加释放城市包含的物品
	bool	addItem(const uint32 & serial);
	bool  	addItem(ResourceProxy & item);
	bool	removeItem(const uint32 & serial);
	bool	removeItem(ResourceProxy & item);
	bool	deleteItem(const uint32 & serial);
	bool	deleteItem(ResourceProxy & item);
	bool	destroyItem(const uint32 & serial);
	bool	destroyItem(ResourceProxy & item);
	void	removeAllItems(void);
	void	destroyAllItems(void);
	void	deleteAllItems(void);

	//添加效果到角色
	bool	addEffect(const uint32 & serial);
	bool	addEffect(ResourceProxy & effect);
	bool	removeEffect(const uint32 & serial);
	bool	removeEffect(ResourceProxy & effect);
	bool	deleteEffect(const uint32 & serial);
	bool	deleteEffect(ResourceProxy & effect);
	bool	destroyEffect(const uint32 & serial);
	bool	destroyEffect(ResourceProxy & effect);
	void	removeAllEffects(void);
	void	destroyAllEffects(void); 
	void	deleteAllEffects(void); 
	//std::list<ResourceProxy> * getEffects(void)	{	return &m_effects;	}
	
	//得到包括特效的列表复制
	std::list<EffectPtr> * getEffectsList(std::list<EffectPtr> * list);
	//得到包含物品的列表复制
	std::list<ItemPtr> * getItemsList(std::list<ItemPtr> * list);
	//得到角色列表
        std::list<CharPtr> * getCharsList(std::list<CharPtr> * list);	

	const String & getTownName(void);

	const TownStatus  getTownStatus(void);
	void setTownStatus(const TownStatus & status);
	//save item to database	
	bool	SaveDB(void);
	bool	LoadDB(void);
	bool    FetchDB(Field * fields);

	void	Delete(void);
	void	Destroy(void);

	//用户数据
        XMLDataSet      user_data;

	//城市公共属性
	town_common_property_t	common;
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to TownPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  TownPtr : public SharedPtr<Town>
    {
    public:
        TownPtr() : SharedPtr<Town>() {}
        explicit TownPtr(Town* rep) : SharedPtr<Town>(rep) {}
        TownPtr(const TownPtr& r) : SharedPtr<Town>(r) {}
        TownPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        TownPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
